#include "MainReactor.h"
#include "LoadBalanceStrategy.h"
#include <iostream>
#include <unistd.h>

using namespace std;

void test_strategy(MainReactor& mainReactor, LoadBalanceStrategy strategy) {
    cout << "\n========== 测试策略 " << static_cast<int>(strategy) << " ==========" << endl;
    mainReactor.setLoadBalanceStrategy(strategy);
    
    // 模拟策略切换
    cout << "当前策略: " << static_cast<int>(mainReactor.getLoadBalanceStrategy()) << endl;
}

int main() {
    cout << "多Reactor负载均衡策略测试" << endl;
    cout << "策略编号: 0=轮询, 1=最少连接, 2=一致性哈希, 3=自适应" << endl;
    
    // 创建MainReactor，4个SubReactor
    MainReactor mainReactor("127.0.0.1", 8888, 4);
    
    // 设置简单的回调
    mainReactor.setAllCallback(
        [](const shared_ptr<TcpConnection>& conn) {
            cout << "新连接建立" << endl;
        },
        [](const shared_ptr<TcpConnection>& conn) {
            cout << "收到消息" << endl;
        },
        [](const shared_ptr<TcpConnection>& conn) {
            cout << "连接关闭" << endl;
        }
    );
    
    // 测试不同策略
    test_strategy(mainReactor, LoadBalanceStrategy::ROUND_ROBIN);
    test_strategy(mainReactor, LoadBalanceStrategy::LEAST_CONNECTIONS);
    test_strategy(mainReactor, LoadBalanceStrategy::CONSISTENT_HASH);
    test_strategy(mainReactor, LoadBalanceStrategy::ADAPTIVE);
    
    cout << "\n服务器启动在端口8888，使用telnet或nc连接测试:" << endl;
    cout << "telnet 127.0.0.1 8888" << endl;
    cout << "按Ctrl+C退出" << endl;
    
    mainReactor.start();
    
    return 0;
}